/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.clazz.security;

import z.h.w.jar.kit.method.Loop;

/**
 *
 * @author EnzoZhong
 */
public class OffsetCode {

      private Integer maxOffset = 5;

      public static OffsetCode born () {
            return new OffsetCode ();
      }

      public String 加密 ( String text ) {
            Loop<Integer> loop = createLoop ();
            StringBuffer stringBuffer = new StringBuffer ();
            Integer len = text.length ();
            for ( Integer x = 0 ; x < len ; x ++ ) {
                  char ch = ( char ) ( ( char ) text.charAt ( x ) + loop.loop () );
                  stringBuffer.append ( ch );
            }
            return stringBuffer.toString ();
      }

      public String 解密 ( String text ) {
            Loop<Integer> loop = createLoop ();
            StringBuffer stringBuffer = new StringBuffer ();
            Integer len = text.length ();
            for ( Integer x = 0 ; x < len ; x ++ ) {
                  char ch = ( char ) ( ( char ) text.charAt ( x ) - loop.loop () );
                  stringBuffer.append ( ch );
            }
            return stringBuffer.toString ();
      }

      private Loop<Integer> createLoop () {
            Loop<Integer> loop = new Loop<> ();
            for ( Integer x = 0 ; x <= maxOffset ; x ++ ) {
                  loop.add ( x );
            }
            return loop;
      }

}
